﻿//438.找到字符串中所有字母异位词
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> ans;
        int hash1[26] = { 0 };
        int hash2[26] = { 0 };

        int size = p.size();
        for (int i = 0; i < size; i++)
        {
            hash1[p[i] - 'a']++;
        }

        int count = 0;
        for (int left = 0, right = 0; right < s.size(); right++)
        {
            hash2[s[right] - 'a']++;

            if (hash2[s[right] - 'a'] <= hash1[s[right] - 'a'])
            {
                count++;
            }

            if (count == size)
            {
                ans.push_back(left);
            }

            if (right - left + 1 == size)
            {
                if (hash2[s[left] - 'a']-- <= hash1[s[left] - 'a'])
                {
                    count--;
                }
                left++;
            }
        }
        return ans;
    }
};